package com.home766.dao.impl;

import com.home766.dao.CasesDao;
import com.home766.entity.Cases;
import com.home766.util.StringUtil;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.dao.NativeBaseDao;
import net.biz.dao.impl.BaseDaoImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("casesDaoImpl")
public class CasesDaoImpl extends BaseDaoImpl<Cases, Long> implements CasesDao{
	@Autowired
	private NativeBaseDao nativeBaseDao;
	@Override
	public Page<Cases> getCasesList(Cases cases, Pageable pageable, String properties) {
		// TODO Auto-generated method stub
		String sql = "select cases.id as id,cases.address_img as addresss_Img,cases.theme as theme,cases.sn as sn,pr.name as product_Category_Name,cases.is_completed as is_Completed,cases.is_release as is_Release,cases.create_date as create_Date"
				+ " from 766_case cases inner join xx_product_category pr on pr.id=cases.product_category_id where 1=1";
		Map<String,Object> tempMap = new HashMap<String, Object>();
		if(cases.getIsCompleted()!=null){
			sql+=" and cases.is_completed =:isCompleted";
			tempMap.put("isCompleted",cases.getIsCompleted());
		}
		if(cases.getIsRelease()!=null){
			sql+=" and cases.is_release =:isRelease";
			tempMap.put("isRelease",cases.getIsRelease());
		}
		if(cases.getProductCategory()!=null){
			sql+=" and cases.product_category_id =:prId";
			tempMap.put("prId",cases.getProductCategory());
		}
		if(StringUtils.isNotEmpty(cases.getHouseType())){
			sql+=" and cases.house_type =:houseType";
			tempMap.put("houseType",cases.getHouseType());
		}
		if(StringUtil.isNotEmpty(properties)){
			sql+=" order by cases."+properties;
		}else{
			sql+=" order by cases.create_date desc";
		}
		return nativeBaseDao.findPage(sql, tempMap, pageable, Cases.class);
	}
	@Override
	public List<Cases> getCasesList() {
		// TODO Auto-generated method stub
		String sql = "select cases.id as id,cases.address_img as addresss_Img,cases.city as city,cases.residential_Quarters as residential_Quarters,cases.sn as sn,pr.name as product_Category_Name,cases.is_completed as is_Completed,cases.is_release as is_Release,cases.create_date as create_Date,"
				+ " cases.scheme_configuration as scheme_Configuration,dv.title as house_Type,cases.theme as theme"
				+ " from 766_case cases inner join xx_product_category pr on pr.id=cases.product_category_id "
				+ " inner join 766_dictionary dv on dv.id = cases.house_type"
				+ " where 1=1 LIMIT 20";
		Map<String,Object> tempMap = new HashMap<String, Object>();
		return nativeBaseDao.findList(sql, tempMap, Cases.class);
	}
	@Override
	public Cases getCases(Long id) {
		// TODO Auto-generated method stub
		String sql = " select cases.id as id,cases.name as name,cases.theme as theme,cases.city as city,cases.residential_quarters as residential_Quarters,cases.user_info as user_Info,"
				+ " cases.scheme_configuration as scheme_Configuration,cases.design_notes as design_Notes,cases.scene_display as scene_Display,pr.name as product_Category_Name,cases.feed_back as feed_Back"
				+ " from 766_case cases inner join xx_product_category pr on pr.id=cases.product_category_id"
				+ " where cases.id=:id ";
		Map<String,Object> tempMap = new HashMap<String, Object>();
		tempMap.put("id",id);
		return nativeBaseDao.findOne(sql, tempMap, Cases.class);
	}
	@Override
	public Page<Cases> getShopCasesList(Cases cases, Pageable pageable) {
		// TODO Auto-generated method stub
		String sql = "select cases.id as id,cases.address_img as addresss_Img,cases.city as city,cases.residential_Quarters as residential_Quarters,cases.sn as sn,pr.name as product_Category_Name,cases.is_completed as is_Completed,cases.is_release as is_Release,cases.create_date as create_Date,"
				+ " cases.scheme_configuration as scheme_Configuration,dv.title as house_Type,cases.theme as theme"
				+ " from 766_case cases inner join xx_product_category pr on pr.id=cases.product_category_id "
				+ " inner join 766_dictionary dv on dv.id = cases.house_type"
				+ " where 1=1";
		Map<String,Object> tempMap = new HashMap<String, Object>();
		if(cases.getHouseType()!=null){
			sql+=" and cases.house_type=:houseType";
			tempMap.put("houseType", cases.getHouseType());
		}
		if(cases.getIsRelease()!=null){
			sql+=" and cases.is_release =:isRelease";
			tempMap.put("isRelease",cases.getIsRelease());
		}
		if(cases.getProductCategory()!=null){
			sql+=" and cases.product_category_id=:productCategory";
			tempMap.put("productCategory", cases.getProductCategory());
		}
		return nativeBaseDao.findPage(sql, tempMap, pageable, Cases.class);
	}
	//@Override
/*	public List<DictionaryValue> getValue() {
		// TODO Auto-generated method stub
		String jpql =" select dictionaryValue from DictionaryValue dictionaryValue where dictionaryValue.typeNo=:typeNo";
		return entityManager.createQuery(jpql,DictionaryValue.class).setFlushMode(FlushModeType.COMMIT).setParameter("typeNo",4).getResultList();
	}*/
}
